package com.imooc.product.controller;

import cn.hutool.Hutool;
import cn.hutool.core.thread.ThreadUtil;
import com.imooc.product.common.DecreaseStockInput;
import com.imooc.product.common.ProductInfoOutput;
import com.imooc.product.dataobject.ProductCategory;
import com.imooc.product.dataobject.ProductInfo;
import com.imooc.product.dto.CartDTO;
import com.imooc.product.service.CategoryService;
import com.imooc.product.service.ProductService;
import com.imooc.product.utils.ResultVOUtil;
import com.imooc.product.vo.ProductInfoVO;
import com.imooc.product.vo.ProductVO;
import com.imooc.product.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;


    @GetMapping("/list")
    @CrossOrigin(allowCredentials = "true") // 实现跨域请求的注解,allowCredentials 允许cookie跨域
    public ResultVO<ProductInfo> list(){
        // 1. 查询所有在架商品
        List<ProductInfo> productInfoList = productService.findUpAll();
        log.info("productInfoList is -> {}",productInfoList);
        // 2. 获取类目type列表
        List<Integer> categoryTypeList = productInfoList.stream().map(ProductInfo::getCategoryType).collect(Collectors.toList());
        log.info("categoryTypeList is -> {}",categoryTypeList);
        // 3. 从数据库里查询类目
        List<ProductCategory> categoryList = categoryService.findByCategoryTypeIn(categoryTypeList);
        log.info("categoryList is -> {}",categoryList);
        // 4. 构造数据
        List<ProductVO> productVOList = new ArrayList<>();

        categoryList.forEach(category->{
            // 封装最外层数据
            ProductVO productVO = new ProductVO();
            productVO.setCategoryName(category.getCategoryName());
            productVO.setCategoryType(category.getCategoryType());
            // 封装分类下包含的产品信息详情
            List<ProductInfoVO> productInfoVOList = new ArrayList<>();
            // 过滤所有含有当前查出分类的产品信息
            productInfoList.forEach(productInfo -> {
                if (productInfo.getCategoryType().equals(category.getCategoryType())){
                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    BeanUtils.copyProperties(productInfo, productInfoVO);
                    productInfoVOList.add(productInfoVO);
                }
            });
            productVO.setProductInfoVOList(productInfoVOList);
            productVOList.add(productVO);
        });
        return ResultVOUtil.success(productVOList);
    }

    /**
     * 获取商品列表(给订单服务用的)
     * @param productIdList
     * @return
     */
    @RequestMapping("/listForOrder")
    public List<ProductInfoOutput> listForOrder(@RequestBody List<String> productIdList){
        ThreadUtil.sleep(2000);

        return productService.findList(productIdList);

    }

    @PostMapping("/decreaseStock")
    public void decreaseStock(@RequestBody List<DecreaseStockInput> decreaseStockInputList){
       productService.decreaseStock(decreaseStockInputList);
    }

    @GetMapping("/testIgnore")
    public Object testIgnore(){

        return "testIgnore";
    }


}
